<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="内涝点编号" prop="waterLogCode">
        <el-input v-model="queryParams.waterLogCode" placeholder="请输入内涝点编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="内涝点名称" prop="pname">
        <el-input v-model="queryParams.pname" placeholder="请输入内涝点名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="内涝点位置" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入内涝点位置" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="排涝时间" prop="drainageTime">
        <el-date-picker clearable size="small" v-model="queryParams.drainageTime" type="date" value-format="yyyy-MM-dd" placeholder="选择排涝时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="现场负责人" prop="scenePerson">
        <el-input v-model="queryParams.scenePerson" placeholder="请输入现场负责人" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['iot:waterLog:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['iot:waterLog:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['iot:waterLog:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['iot:waterLog:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waterLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="内涝点编号" align="center" prop="waterLogCode" />
      <el-table-column label="内涝点名称" align="center" prop="pname" />
      <el-table-column label="经度" align="center" prop="lng" />
      <el-table-column label="纬度" align="center" prop="lat" />
      <el-table-column label="内涝点位置" align="center" prop="address" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="水深" align="center" prop="deep" />
      <el-table-column label="降雨量" align="center" prop="rain" />
      <el-table-column label="出动人次" align="center" prop="manTime" />
      <el-table-column label="积水原因" align="center" prop="cause" />
      <el-table-column label="采取措施" align="center" prop="takeStep" />
      <el-table-column label="排涝时间" align="center" prop="drainageTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.drainageTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退水时间" align="center" prop="backwaterTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.backwaterTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="影响及损失" align="center" prop="impactLoss" />
      <el-table-column label="现场负责人" align="center" prop="scenePerson" />
      <el-table-column label="内涝区域是否绘制" align="center" prop="drawArea" />
      <el-table-column label="备注说明" align="center" prop="note" />
      <el-table-column label="上报时间" align="center" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="operator" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['iot:waterLog:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['iot:waterLog:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改内涝点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="form_default">
        <div class="form_item_head">基础信息</div>
        <el-form-item label="内涝点编号" prop="waterLogCode">
          <el-input v-model="form.waterLogCode" placeholder="请输入内涝点编号" />
        </el-form-item>
        <el-form-item label="内涝点名称" prop="pname">
          <el-input v-model="form.pname" placeholder="请输入内涝点名称" />
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="内涝点位置" prop="address">
          <el-input v-model="form.address" placeholder="请输入内涝点位置" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="水深" prop="deep">
          <el-input v-model="form.deep" placeholder="请输入水深" />
        </el-form-item>
        <el-form-item label="降雨量" prop="rain">
          <el-input v-model="form.rain" placeholder="请输入降雨量" />
        </el-form-item>
        <el-form-item label="积水原因" prop="cause" class="width100">
          <el-input v-model="form.cause" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <div class="form_item_head">处理信息</div>
        <el-form-item label="出动人次" prop="manTime">
          <el-input v-model.number="form.manTime" placeholder="请输入出动人次" />
        </el-form-item>
        <el-form-item label="采取措施" prop="takeStep">
          <el-input v-model="form.takeStep" placeholder="请输入采取措施" />
        </el-form-item>
        <el-form-item label="排涝时间" prop="drainageTime">
          <el-date-picker clearable size="small" v-model="form.drainageTime" type="date" value-format="yyyy-MM-dd" placeholder="选择排涝时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退水时间" prop="backwaterTime">
          <el-date-picker clearable size="small" v-model="form.backwaterTime" type="date" value-format="yyyy-MM-dd" placeholder="选择退水时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="现场负责人" prop="scenePerson">
          <el-input v-model="form.scenePerson" placeholder="请输入现场负责人" />
        </el-form-item>
        <el-form-item label="区域绘制" prop="drawArea">
          <el-radio-group v-model="form.drawArea">
            <el-radio label="是"></el-radio>
            <el-radio label="否"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上报时间" prop="reportTime">
          <el-date-picker clearable size="small" v-model="form.reportTime" type="date" value-format="yyyy-MM-dd" placeholder="选择上报时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人员" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人员" />
        </el-form-item>
        <el-form-item label="影响及损失" prop="impactLoss" class="width100">
          <el-input v-model="form.impactLoss" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注说明" prop="note" class="width100">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWaterLog,
  getWaterLog,
  delWaterLog,
  addWaterLog,
  updateWaterLog,
  exportWaterLog,
} from "@/api/iot/waterLog";

export default {
  name: "WaterLog",
  data() {
    var isnumber = (rule, value, callback) => {
      if (!value) {
        callback(new Error("该值不能为空"));
      } else {
        var reg = /^-?\d{1,18}(?:\.\d{1,3})?$/;
        if (reg.test(value)) {
          callback();
        } else {
          callback(
            new Error("输入正确的数字,小数点前1到18位，小数点后可1到3位")
          );
        }
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内涝点表格数据
      waterLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        waterLogCode: null,
        pname: null,
        lng: null,
        lat: null,
        address: null,
        area: null,
        deep: null,
        rain: null,
        manTime: null,
        cause: null,
        takeStep: null,
        drainageTime: null,
        backwaterTime: null,
        impactLoss: null,
        scenePerson: null,
        drawArea: null,
        note: null,
        reportTime: null,
        operator: null,
        isSure: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        drainageTime: [
          {
            required: true,
            message: "请选择排涝时间",
            trigger: "change",
          },
        ],
        backwaterTime: [
          {
            required: true,
            message: "请选择退水时间",
            trigger: "change",
          },
        ],
        reportTime: [
          {
            required: true,
            message: "请选择上报时间",
            trigger: "change",
          },
        ],
        manTime: [
          { required: true, message: "出动人次不能为空" },
          { type: "number", message: "必须为数字值" },
        ],
        waterLogCode: [
          { required: true, message: "内涝点编号不能为空", trigger: "blur" },
        ],
        pname: [
          { required: true, message: "内涝点名称不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "内涝点位置不能为空", trigger: "blur" },
        ],
        area: [
          {
            validator: isnumber,
            required: true,
            trigger: "blur",
          },
        ],
        deep: [{ validator: isnumber, required: true, trigger: "blur" }],
        rain: [{ validator: isnumber, required: true, trigger: "blur" }],
        cause: [
          { required: true, message: "积水原因不能为空", trigger: "blur" },
        ],
        takeStep: [
          { required: true, message: "采取措施不能为空", trigger: "blur" },
        ],
        impactLoss: [
          { required: true, message: "影响及损失不能为空", trigger: "blur" },
        ],
        scenePerson: [
          { required: true, message: "现场负责人不能为空", trigger: "blur" },
        ],
        drawArea: [
          {
            required: true,
            message: "内涝区域是否绘制不能为空",
            trigger: "blur",
          },
        ],
        note: [
          { required: true, message: "备注说明不能为空", trigger: "blur" },
        ],
        operator: [
          { required: true, message: "操作人员不能为空", trigger: "blur" },
        ],
        isSure: [{ required: true, message: "确认不能为空", trigger: "blur" }],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "更新者不能为空", trigger: "blur" },
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询内涝点列表 */
    getList() {
      this.loading = true;
      listWaterLog(this.queryParams).then((response) => {
        this.waterLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        waterLogId: null,
        waterLogCode: null,
        pname: null,
        lng: null,
        lat: null,
        address: null,
        area: null,
        deep: null,
        rain: null,
        manTime: null,
        cause: null,
        takeStep: null,
        drainageTime: null,
        backwaterTime: null,
        impactLoss: null,
        scenePerson: null,
        drawArea: null,
        note: null,
        reportTime: null,
        operator: null,
        isSure: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.waterLogId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加内涝点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const waterLogId = row.waterLogId || this.ids;
      getWaterLog(waterLogId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改内涝点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.waterLogId != null) {
            updateWaterLog(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWaterLog(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const waterLogIds = row.waterLogId || this.ids;
      this.$confirm(
        '是否确认删除内涝点编号为"' + waterLogIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delWaterLog(waterLogIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有内涝点数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportWaterLog(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
